package com.tengju.payment.domain.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;


/**
 * 全局通用交易状态
 */
@AllArgsConstructor
public enum TradeStatus {

    CREATED(10, "已创建"),

    PROCESS(20, "处理中"),

    SUCCESS(30, "成功"),

    FAIL(40, "失败"),

    DISHONOUR(50, "退票"),
    ;


    private final int val;
    private final String desc;

    @JsonValue
    public int getVal() {
        return val;
    }

    public String getDesc() {
        return desc;
    }

    @JsonCreator
    public static TradeStatus fromVal(int val) {
        for (TradeStatus data : values()) {
            if (data.val == val) {
                return data;
            }
        }
        return null;
    }

    public boolean isComplete() {

        return TradeStatus.SUCCESS == this || TradeStatus.FAIL == this;
    }

    // todo 加入退票逻辑
    public boolean canChangeTo(TradeStatus targetStatus) {
        boolean canChange;
        switch (this) {
            case CREATED:
                canChange = targetStatus == PROCESS || targetStatus == SUCCESS || targetStatus == FAIL;
                break;
            case PROCESS:
                canChange = targetStatus == SUCCESS || targetStatus == FAIL;
                break;
            case SUCCESS:
            case FAIL:
            default:
                canChange = false;
        }
        return canChange;
    }
}
